#define _CRT_SECURE_NO_WARNINGS 1





#include <iostream>
#include<map>
#include<vector>
using namespace std;
const int N = 1e4 + 10;
int a, b, ap[N];
int isprime(int a)
{
    if (a <= 1)
        return 1;
    for (int i = 2;i * i <= a;i++)
        if (a % i == 0)
            return 1;
    return 2;
}
int main()
{
    cin >> a >> b;
    map<int, int> res;
    for (int i = a;i <= b;i++)
    {
        vector<int> vv;
        int sum = 0, x = i;
        while (x != 1)
        {
            sum = 0;
            while (x)
            {
                sum += pow(x % 10, 2);
                x /= 10;
            }
            x = sum;
            if (find(vv.begin(), vv.end(), sum) != vv.end())
                break;
            vv.push_back(sum);
            ap[sum] = 1;
        }
        if (sum == 1)
            res[i] = vv.size();
    }
    int flag = 0;
    for (auto it = res.begin();it != res.end();it++)
    {
        if (!ap[it->first])
        {
            cout << it->first << " " << it->second * isprime(it->first) << endl;
            flag = 1;
        }
    }
    if (flag == 0)
        cout << "SAD";
    return 0;
}
